package no.group.gowther.service;

import no.group.gowther.enums.FileType;
import no.group.gowther.exception.BusinessException;
import no.group.gowther.exception.ErrorMessage;
import no.group.gowther.service.dto.ImageCode;
import no.group.gowther.util.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ImageServiceImpl implements ImageService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ImageServiceImpl.class);

    @Override
    public ImageCode generateImageCode(Path imagePath) {
        return new ImageCode(imagePath);
    }

    @Override
    public List<ImageCode> generateDirImageCode(Path dirPath) {
        try {
            return FileUtils.listFileBy(dirPath, FileType.IMAGE::isMyType).stream()
                    .map(this::generateImageCode)
                    .collect(Collectors.toList());
        } catch (IOException e) {
            LOGGER.error("获取目录[{}]下文件失败", dirPath, e);
            throw BusinessException.createException(ErrorMessage.SYSTEM_ERROR.getErrorCode(), "获取目录下文件失败");
        }
    }

    @Override
    public Path decodeImageCode(String imageCode) {
        return new ImageCode(imageCode).decodeImageCode();
    }
}
